#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Patcher_Icon.ico
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstants.au3>
#include <GDIpProgress.au3>
#include <File.au3>
#include <Crypt.au3>
#include <Array.au3>

OnAutoItExitRegister("_Exit")

$link = "www.google.com" ; Hier kommt die URL zur Patchlist und zu den Dateien hin

Local $read, $files[1][2], $dir, $szDrive, $szDir, $szName, $szExt, $toload = 0, $loaded = 0, $trans = 0
Local $wishexe = @ScriptDir & "\Wish.exe"

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("WISH Patcher", 506, 171, -1, -1, $WS_POPUPWINDOW)
GUISetBkColor(0x212121)
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\Images\Hintergrund.jpg", 0, 0, 528, 185, $WS_CLIPCHILDREN)
$Button1 = GUICtrlCreateButton("Start", 224, 120, 75, 25)
GUICtrlSetState($Button1, $GUI_DISABLE)
$Progress1 = GUICtrlCreateProgress(8, 72, 486, 15)
$Progress2 = GUICtrlCreateProgress(8, 96, 486, 15)
$Icon1 = GUICtrlCreateIcon(@ScriptDir & "\Images\Close_Icon.ico", -1, 490, 0, 16, 16)
$Icon2 = GUICtrlCreateIcon(@ScriptDir & "\Images\Minimize_Icon.ico", -1, 470, 0, 16, 16)
$Label1 = GUICtrlCreateLabel("Compare Files...", 8, 52, 473, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Verdana")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
WinSetTrans($Form1,"",0)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

AdlibRegister("_Fade",10)

Ping($link)
If @error <> 0 Then
	Exit MsgBox(16, "Error", "Internet Error:" & @error)
EndIf
$link &= "/"
_Crypt_Startup()

If FileExists(@ScriptDir & "\FileList.txt") Then FileDelete(@ScriptDir & "\FileList.txt")
Sleep(1000)
$read = BinaryToString(InetRead($link & "FileList.txt"))
$read = StringSplit($read, @CRLF)
_ArrayDelete($read, 0)
ReDim $files[UBound($read)][4]



For $i = 0 To UBound($read) - 1 Step +1
	If StringInStr($read[$i], "|") Then
		$split = StringSplit($read[$i], "|")
		$files[$i][0] = StringReplace($split[1], "\", "/")
		$files[$i][1] = $split[1]
		$files[$i][2] = $split[2]

		If Not FileExists(@ScriptDir & $files[$i][1]) Then
			$files[$i][3] = 1
		Else
			If _Crypt_HashFile(@ScriptDir & $files[$i][1], $CALG_SHA1) <> $files[$i][2] Then
				$files[$i][3] = 1
			Else
				$files[$i][3] = 0
			EndIf
		EndIf

		If $files[$i][3] = 1 Then
			$toload += 1
		EndIf

		_PathSplit(@ScriptDir & $files[$i][1], $szDrive, $szDir, $szName, $szExt)
		If Not FileExists($szDrive & $szDir) Then
			DirCreate($szDrive & $szDir)
		EndIf
	EndIf
Next

_Crypt_Shutdown()

Sleep(2000)
GUICtrlSetData($Label1, "Downloading Files...")
Sleep(2000)

For $i = 0 To UBound($files) - 1 Step +1
	If $files[$i][3] = 1 Then
		GUICtrlSetData($Label1, "Downloading : " & $files[$i][0])
		$cSize = InetGetSize($link & "/patch/" & $files[$i][0])
		$iHandle = InetGet($link & "/patch" & $files[$i][0], @ScriptDir & $files[$i][1], 1, 1)
		Do
			$bLoaded = InetGetInfo($iHandle)
			GUICtrlSetData($Progress1, Round($bLoaded[0] / $cSize * 100))
			$nMsg = GUIGetMsg()
			Switch $nMsg
				Case $GUI_EVENT_CLOSE
					InetClose($iHandle)
					Exit
				Case $Icon1
					InetClose($iHandle)
					Exit
				Case $Icon2
					GUISetState(@SW_MINIMIZE, $Form1)
			EndSwitch
		Until $bLoaded[2]
		$loaded += 1
		GUICtrlSetData($Progress2, $loaded / $toload * 100)
	EndIf
	GUICtrlSetData($Progress1, 0)
Next
GUICtrlSetData($Progress1, 100)
GUICtrlSetData($Progress2, 100)
GUICtrlSetState($Button1, $GUI_ENABLE)
GUICtrlSetData($Label1, "Ready to Start")



While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $Button1
			Run($wishexe)
			Exit
		Case $Icon1
			Exit
		Case $Icon2
			GUISetState(@SW_MINIMIZE, $Form1)
	EndSwitch
WEnd

Func _Exit()
	_Crypt_Shutdown()
	For $i = 255 To 0 Step -1
		WinSetTrans($Form1, "", $i)
		Sleep(5)
	Next
EndFunc   ;==>_Exit

Func _Fade()
	$trans += 1
	If $trans > 255 Then
		AdlibUnRegister("_Fade")
		Return 0
	EndIf
	WinSetTrans($Form1, "", $trans)
EndFunc   ;==>_Fade
